<?php
namespace frontend\controllers;

use common\models\Article;
use common\models\ArticleTo;
use common\models\Catalog;
use common\models\Meta;
use common\models\Post;
use common\utils\XCache;
use common\utils\XMap;
use Yii;
use yii\data\ActiveDataProvider;

/**
 * 目录控制器
 */
class CatalogController extends BaseController
{
    public function actionIndex($alias)
    {
        Yii::$app->view->params['alias'] = $alias;
        //$model = Catalog::findOne(['alias' => $alias]);
        $model = XCache::catalog($alias);
        if (!$model) {
            $this->tips('该栏目已经不存在');
        }
        if ($model->type == 3) {
            //外链
            $this->redirect($model->link, 301);
        } elseif ($model->type == 2) {
            //图文
            $view_show = $model->view_show;
            if (!$view_show) {
                $this->tips('该视图已经不存在');
            }
            $this->layout = XMap::layoutCatalog($view_show);
            //素材
            $article = [];
            $articleTo = ArticleTo::findOne(['type' => 'catalog', 'pk' => $model->id]);
            if ($articleTo) {
                $article = Article::findOne($articleTo->article_id);
                if ($article) {
                    if ($article->is_link) {
                        //素材是外链
                        $this->redirect($article->link, 301);
                    }
                }
            }

            $meta = Meta::get($model->tableName(), $model->id);
            if ($meta->id) {
                $this->view->title = $meta->title;
                $this->setSeo($meta->keywords, $meta->description);
            } else {
                $this->view->title = $model->name;
            }

            return $this->xrender($view_show, ['catalog' => $model, 'article' => $article]);
        } elseif ($model->type == 1) {
            //目录
            $view_list = $model->view_list;
            if (!$view_list) {
                $this->tips('该视图已经不存在');
            }
            $this->layout = XMap::layoutCatalog($view_list);
            $itemView = '/items/';
//            if (!$model->view_item) {
//                $this->tips('未设置目录列表块');
//            }
            if ($model->view_item&&!file_exists(Yii::getAlias('@frontend' . '/views/items/') . $model->view_item . '.php')) {
                $this->tips('该目录列表块已经不存在');
            }
            $itemView .= $model->view_item;

            $query = Post::find()->with('attr');
            //获取$catalogId子类
            $childIdes = $model->getChildIdes($model->id);
            if ($childIdes) {
                $ids = $model->id . ',' . $childIdes;
                $query->where("catalog_id in($ids)");
            } else {
                $query->where("catalog_id=$model->id");
            }
            if (Yii::$app->request->get('title')) {
                $query->andWhere(['like', 'title', Yii::$app->request->get('title')]);
            }
            $query->orderBy("recommend desc,sort asc,id desc");
            $page_size=$model->page_size ? $model->page_size : 20;
            $dataProvider = new ActiveDataProvider([
                'query' => $query,
                'pagination' => [
                    'pageSize' => $page_size,
                ],
            ]);

            $meta = Meta::get($model->tableName(), $model->id);
            if ($meta->id) {
                $this->view->title = $meta->title;
                $this->setSeo($meta->keywords, $meta->description);
            } else {
                $this->view->title = $model->name;
            }
            return $this->xrender($view_list, [
                'catalog' => $model,
                'dataProvider' => $dataProvider,
                'itemView' => $itemView,
                'page_size'=>$page_size
            ]);
        }
    }
}
